﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using com.sense.cloud.common;

namespace com.sense.cloud.demo
{
    class OpenAPIDemoByCSharp
    {
        // Virbox LM 平台 OpenAPI V2 应用基地址
        private static string appUrl = "https://openapi.senseyun.com";
        // 使用时应换成自己的 appId 和 appSecret.
        // 如果查看 appId/appSecret 请查看文档：https://developer.lm.virbox.com/course/course.html#other
        private static string appId = "0b0c24be2c614fa48c2067aedc43ec71";
        private static string appSecret = "1432c2b485104a9ebecd2019703613e3";
        // 初始化 api 请求
        private static API2Request api2Request = new API2Request(appUrl, appId, appSecret);

        // 声明测试过程中的中间变量，因为某些接口的参数依赖指定接口的返回数据
        // 普通用户的 userId
        private static string userid = null;
        // 影子账号 account
        private static string shadowAccountWithDeveloperGuid = null;
        // 影子账号的 userId
        private static string shadowAccountUserId = null;
        // 软许可模板 id
        private static string tidofsoft = null;
        // 云许可模板 id
        private static string tidofcloud = null;
        // 软许可的唯一 guid
        private static string licenseGuid = null;
        // 标签 id
        private static string tagid = null;
        // 声明中间变量结束


        // 获取当前时间戳
        private static double  now = Math.Round((DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds);

        // 声明测试过程中用到的一些数据
        // 截取当前时间，用来模拟手机号，手机号必须是1开头的11位数字。
        // 正式使用时请使用正确的手机号，否则会影响业务使用
        private static string userAccount = "1" + now.ToString();
        // 影子账号
        private static string shadowAccount = "OpenAPIV2-" + now.ToString();
        // 产品id, 在此测试使用固定的 id.
        private static int licenseId = 17;
        // 声明测试数据结束

        static void Main(string[] args)
        {
            addUser();
            findUser();
            modifyUserPassword();           
            modifyUser();
            addShadowUser();
            modifyShadowUserPassword();


            addProduct();
            modifyProduct();
            getProductInfo();
            productList();

            addTemplateOfCloud();
            addTemplateOfSoft();
            modifyTemplate();
            templateList();
            findTemplate();

            issueLicense();
            issueLicenseToShadowAccount();
            issueLicenseByProduct();
            licenseList();
            updateLicense();
            licenseUseInfo();
            softLockbindList();
            softLockbindHistory();


            addTag();
            updateTag();
            findTags();
            deleteTag();

            deleteTemplateOfCloud();
            deleteTemplateOfSoft();
            deleteProduct();
            deleteShadowUser();
            deleteUser();

            Console.WriteLine("执行完成");
            Console.ReadLine();

        }

        // 测试普通账号开始

        /// <summary>
        /// 添加普通用户
        /// </summary>
        private static void addUser()
        {

            JObject userJson = new JObject
            {
                {"userAccount",userAccount },
                {"nickname", "kanghl"},
                {"passwd","123456" },
                {"accountType",2 }
            };
            JObject resultJson = api2Request.send("/v2/sv/addUser", userJson);
            userid = resultJson["data"]["userId"].ToString();
        }

        /// <summary>
        /// 查找用户
        /// </summary>
        private static void findUser()
        {
            JObject userJson = new JObject
            {
                //{"cellphone",userAccount }
                {"cellphone","11533193840" }

            };
            JObject resultJson = api2Request.send("/v2/sv/findUsers", userJson);
            if (resultJson["data"].Value<Int32>("totalCount")<1)
            {
                throw new Exception("未找到预期用户");
            }
            
        }

        /// <summary>
        /// 修改用户密码，因为只能修改影子账号的密码，所以普通用户的密码无法修改
        /// </summary>
        private static void modifyUserPassword()
        {
            JObject userJson = new JObject
            {
                //{"cellphone",userAccount }
                {"userAccount","11533193840" },
                {"passwd","12345678" }

            };
            JObject resultJson = api2Request.send("/v2/sv/modifyUserPasswd", userJson);
            Console.WriteLine(resultJson.ToString());
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        private static void modifyUser()
        {
            JObject userJson = new JObject
            {
                {"userId", userid },
                {"desc","修改用户信息" }
            };
            JObject resultJson = api2Request.send("/v2/sv/modifyUser", userJson);
            Console.WriteLine(resultJson.ToString());
        }
  
        // 测试普通账号结束

        // 测试影子账号开始

        private static void addShadowUser()
        {
            JObject userJson = new JObject
            {
                {"shadowAccount",shadowAccount },
                {"nickname","kanghl" },
                {"passwd","123456" }
            };

            JObject resultJson = api2Request.send("/v2/sv/addShadowUser", userJson);
            shadowAccountWithDeveloperGuid = resultJson["data"].Value<string>("userAccount");
            shadowAccountUserId = resultJson["data"].Value<string>("userId");
        }
        /// <summary>
        /// 修改影子账号密码
        /// </summary>
        private static void modifyShadowUserPassword()
        {
            JObject userJson = new JObject
            {
                {"cellphone",shadowAccountWithDeveloperGuid },               
                {"passwd","12345678" }

            };
            JObject resultJson = api2Request.send("/v2/sv/modifyUserPasswd", userJson);
            Console.WriteLine(resultJson.ToString());
        }

        // 测试影子账号结束

        // 测试产品开始

        private static void addProduct()
        {
           JObject productJson = new JObject
           {
                {"licenseId",licenseId},
                {"productName","测试API2.0" },
                {"licenseForm",new JArray { 1,2 } },
                {"pub","1234" },
                {"rom","123456" },
                {"raw","12345678" },
                {"modules",
                    new JArray {
                        new JObject
                        {
                            {"moduleId",1 },
                            {"moduleName","高级功能1" }
                        },
                        new JObject
                        {
                            {"moduleId",2 },
                            {"moduleName","高级功能2" }
                        },
                    }
                }

           };
            JObject resultJson = api2Request.send("/v2/sv/addProduct", productJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void modifyProduct()
        {
            JObject productJson = new JObject
            {
                {"licenseId",licenseId },
                {"productName","测试API2.0修改产品名称" }
            };
            JObject resultJson = api2Request.send("/v2/sv/modifyProduct", productJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void getProductInfo()
        {
            JObject productJson = new JObject
            {
                {"licenseId",licenseId }
            };
            JObject resultJson = api2Request.send("/v2/sv/getProductInfo", productJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void productList()
        {
            JObject productJson = new JObject
            {
                {"productName","API2" }
            };
            JObject resultJson = api2Request.send("/v2/sv/productList", productJson);
            Console.WriteLine(resultJson.ToString());
        }

        // 测试产品结束

        // 测试模板开始

        /// <summary>
        /// 添加云锁模板
        /// </summary>
        private static void addTemplateOfCloud()
        {
            JObject templateJson = new JObject
            {
                {"templateName","测试API2.0-云锁" },
                {"licenseId",licenseId },
                {"licenseForm",1 },
                {"isForever",true },
                {"canModify",1 },
                {"modules",
                    new JArray
                    {
                        new JObject
                        {
                            {"moduleId",1 },
                            {"moduleName","高级功能1" }
                        }
                    }
                }
            };
            JObject resultJson = api2Request.send("/v2/sv/addTemplate", templateJson);
            tidofcloud = resultJson["data"].Value<string>("templateId");
            Console.WriteLine(resultJson.ToString());
        }


        private static void addTemplateOfSoft()
        {
            JObject templateJson = new JObject
            {
                {"templateName","测试API2.0-软锁" },
                {"licenseId",licenseId },
                {"licenseForm",2 },
                {"isForever",false },
                {"licStartTimeType",0 },
                {"startTime", now},
                {"endTime",now+1000 },
                {"offlineDays",2 },
                {"licBindLimit",2 },
                {"licBindMaxLimit",2 },
                {"licenseMode",0 },
                {"pricingMode","yearly" },
                {"canModify",1 },
                {"modules",
                    new JArray
                    {
                        new JObject
                        {
                            {"moduleId",1 },
                            {"moduleName","高级功能1" }
                        }
                    }
                }
            };
            JObject resultJson = api2Request.send("/v2/sv/addTemplate", templateJson);
            tidofsoft = resultJson["data"].Value<string>("templateId");
            Console.WriteLine(resultJson.ToString());
        }

        private static void modifyTemplate()
        {
            JObject templateJson = new JObject
            {
                {"templateId",tidofsoft },
                {"templateName","测试API2.0-软锁-修改名称" },
                {"licenseForm",2 },
                {"isForever",true },
                {"offlineDays",2 },
                {"licBindLimit",2 },
                {"licBindMaxLimit",2 },
                {"licenseMode",0 },
                {"pricingMode","once" },
                {"canModify",1 }
            };
            JObject resultJson = api2Request.send("/v2/sv/modifyTemplate", templateJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void templateList()
        {
            JObject templateJson = new JObject
            {
                {"licenseId",licenseId }
            };
            JObject resultJson = api2Request.send("/v2/sv/templateList", templateJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void findTemplate()
        {
            JObject templateJson = new JObject
            {
                {"templateId",tidofsoft }
            };
            JObject resultJson = api2Request.send("/v2/sv/findTemplate", templateJson);
            Console.WriteLine(resultJson.ToString());
        }

        // 测试模板结束

        // 测试许可开始

        private static void issueLicense()
        {
            JObject licenseJson = new JObject
            {
                {"templateId",tidofcloud },
                {"userAccount",userAccount }
            };
            JObject resultJson = api2Request.send("/v2/sv/issueLicense", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void issueLicenseToShadowAccount()
        {
            JObject licenseJson = new JObject
            {
                {"templateId",tidofcloud },
                {"userAccount",shadowAccountWithDeveloperGuid }
            };
            JObject resultJson = api2Request.send("/v2/sv/issueLicense", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void issueLicenseByProduct()
        {
            JObject licenseJson = new JObject
            {
                {"licenseId",licenseId },
                {"licenseForm",2 },
                {"isForever",false },
                {"userAccount", userAccount },
                {"version",2 },
                {"startTime", now},
                {"endTime",now+1000 },
                {"span",3 },
                {"offlineDays",3 },
                {"licBindLimit",3 },
                {"licBindMaxLimit",3 },
                {"isOffline",true },
                {"licenseMode",0 },
                {"pricingMode","yearly" }
            };
            JObject resultJson = api2Request.send("/v2/sv/issueLicenseByProduct", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void licenseList()
        {
            JObject licenseJson = new JObject
            {
                {"licenseForms",new JArray {2}},
                {"userAccount",userAccount }
            };
            JObject resultJson = api2Request.send("/v2/sv/licenseList", licenseJson);
            licenseGuid = resultJson["data"]["licenses"][0]["pubLicenseGuid"].ToString();
            Console.WriteLine(resultJson.ToString());
        }

        private static void updateLicense()
        {
            JObject licenseJson = new JObject
            {
                {"licenseId",licenseId },
                {"licenseForm",2 },
                {"userAccount", userAccount },
                {"version",
                    new JObject
                    {
                        {"op","set"},
                        {"value",4 }
                    }
                },
                {"span",
                    new JObject
                    {
                        {"op","set"},
                        {"value",4 }
                    }
                }
               
            };
            JObject resultJson = api2Request.send("/v2/sv/updateLicense", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void licenseUseInfo()
        {
            JObject licenseJson = new JObject
            {
                {"licenseId",licenseId },
                {"licenseForm",2 },
                {"userAccount", userAccount }              

            };
            JObject resultJson = api2Request.send("/v2/sv/licenseUseInfo", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void softLockbindList()
        {
            JObject licenseJson = new JObject
            {
                {"licenseGuid",licenseGuid }
            };
            JObject resultJson = api2Request.send("/v2/sv/softLockbindList", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void softLockbindHistory()
        {
            JObject licenseJson = new JObject
            {
                {"licenseGuid",licenseGuid }
            };
            JObject resultJson = api2Request.send("/v2/sv/softLockbindHistory", licenseJson);
            Console.WriteLine(resultJson.ToString());
        }

        // 测试许可结束

        // 测试标签开始

        private static void addTag()
        {
            JObject tagJson = new JObject
            {
                {"name","测试标签" },
            };
            JObject resultJson = api2Request.send("/v2/sv/addTag", tagJson);
            Console.WriteLine(resultJson.ToString());
            tagid = resultJson["data"].Value<string>("id");
        }

        private static void updateTag()
        {
            JObject tagJson = new JObject
            {
                {"name","测试标签修改" },
                {"id",tagid }
            };
            JObject resultJson = api2Request.send("/v2/sv/updateTag", tagJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void findTags()
        {
            JObject tagJson = new JObject
            {
                {"name","测试" }
            };
            JObject resultJson = api2Request.send("/v2/sv/findTags", tagJson);
            Console.WriteLine(resultJson.ToString());
        }

        private static void deleteTag()
        {
            JObject tagJson = new JObject
            {
                {"id",tagid}
            };
            JObject resultJson = api2Request.send("/v2/sv/deleteTag", tagJson);
            Console.WriteLine(resultJson.ToString());
        }
        // 测试标签结束


        // 开始清理数据
        // 主要清理用户、产品、模板

        private static void deleteTemplateOfCloud()
        {
            JObject templateJson = new JObject
            {
                {"templateId",tidofcloud }
            };

            JObject resultJson = api2Request.send("/v2/sv/deleteTemplate", templateJson);
            Console.WriteLine(resultJson);
        }
        private static void deleteTemplateOfSoft()
        {
            JObject templateJson = new JObject
            {
                {"templateId",tidofsoft }
            };

            JObject resultJson = api2Request.send("/v2/sv/deleteTemplate", templateJson);
            Console.WriteLine(resultJson);
        }

        private static void deleteProduct()
        {
            JObject productJson = new JObject
            {
                {"licenseId",licenseId }
            };

            JObject resultJson = api2Request.send("/v2/sv/deleteProduct", productJson);
            Console.WriteLine(resultJson);
        }

        private static void deleteShadowUser()
        {
            JObject userJson = new JObject
            {
                {"userId",shadowAccountUserId }
            };

            JObject resultJson = api2Request.send("/v2/sv/deleteUser", userJson);
            Console.WriteLine(resultJson);
        }
        private static void deleteUser()
        {
            JObject userJson = new JObject
            {
                {"userId",userid }
            };

            JObject resultJson = api2Request.send("/v2/sv/deleteUser", userJson);
            Console.WriteLine(resultJson);
        }

    }
}
